//
//  IPCreatJobPlanController.swift
//  IPlan
//
//  Created by robot on 3/24/16.
//  Copyright © 2016 codeFighter.com. All rights reserved.
//

import UIKit

class IPCreatJobPlanController: IPCreatNewPlanController {

    fileprivate var jobPlanSelectTimeView: IPInputPlanSelectTimeView?
    fileprivate var isHaveContent:Bool = false
    fileprivate var jobPlanclickedTimeIndex:Int?
    fileprivate var arrOfRewardDetail = ["请选择计划时间","请填写工作内容","提醒时间"]
    lazy var shopPlanTableView = UITableView()
    let jobSelectTimeFrect: CGRect = CGRect(x: 0, y: 10, width: SCREEN_W, height: SCREEN_H)

    fileprivate var creatJobContentTextHeight:CGFloat = 0.0 // Job contents's height

    // MARK: life cycle
    override func viewDidLoad() {
        
        super.viewDidLoad()
        title = "新建工作计划"
        view.backgroundColor = UIColor.white
        setUpRightBarButtonItem()
        //setUpSelectTimeContentView()
        registerNotificationCenter()
        view.addSubview(shopPlanTableView)
        checkThePlanContent()
        shopPlanTableView.frame = CGRect(x: 0, y: 0, width: SCREEN_W, height: SCREEN_H - 64)
        shopPlanTableView.delegate = self
        shopPlanTableView.dataSource = self
        
    }
    
    func setUpSelectTimeContentView (){
       
     
        let willBeExecuteClosure:(_ string:String) -> Void = {
           
          [weak self]  (timeString:String) -> Void in
            
            self!.arrOfRewardDetail[self!.jobPlanclickedTimeIndex!] = timeString
            let indexPathArray:Array = [IndexPath.init(row: self!.jobPlanclickedTimeIndex!, section: 0)]
            self!.shopPlanTableView.reloadRows(at: indexPathArray, with: UITableViewRowAnimation.none)
            self!.jobPlanSelectTimeView?.removeFromSuperview()

        }
         jobPlanSelectTimeView = IPInputPlanSelectTimeView(frame: jobSelectTimeFrect, inputSelectTimeClosure: willBeExecuteClosure)
        view.insertSubview(jobPlanSelectTimeView!, at: 10)
    }
    // delegate func
    func inputPlanSelectTime(_ selectString: String) {
        
        print(selectString)
        
    }
    
    func setUpRightBarButtonItem() ->Void{
        
        let rightBarButtonItem:UIBarButtonItem = UIBarButtonItem.init(title: "完成", style: UIBarButtonItemStyle.bordered, target: self, action: #selector(IPCreatJobPlanController.finishButtonClicked(_:)))
        rightBarButtonItem.tintColor = UIColor.red
        navigationItem.rightBarButtonItem = rightBarButtonItem
        
    }
    func registerNotificationCenter() ->Void{
    
       NotificationCenter.default.addObserver(self, selector: #selector(IPCreatJobPlanController.checkThePlanContent), name: NSNotification.Name(rawValue: JOB_CONTENT_CHANGED_IDENTIFIER), object: nil)
        
    
    }
    // chek if there is planContent
    func checkThePlanContent() ->Void{
    
        let contentString: String? = UserDefaults.standard.object(forKey: "jobContent") as? String
        
        creatJobContentTextHeight = (contentString?.stringHeightWithFont(UIFont.systemFont(ofSize: 15), contentWidth: SCREEN_W))!
        
        if contentString != nil{

            arrOfRewardDetail.insert(contentString!, at: 2)
            isHaveContent = true
            shopPlanTableView.insertRows(at: [IndexPath.init(row: 2, section: 0)], with: UITableViewRowAnimation.bottom)
            
        }
      
    
    }
    
    
    func finishButtonClicked(_ clickedButton:UIButton) ->Void {
        
        
        navigationController?.popViewController(animated: true)
        
        
    }
}
// MARK: UITableView Delegate and DataSource
extension IPCreatJobPlanController: UITableViewDataSource,UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return arrOfRewardDetail.count
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        if (isHaveContent && (indexPath as NSIndexPath).row == 2){
        
            let contentString: String? = UserDefaults.standard.object(forKey: "jobContent") as? String
            
            creatJobContentTextHeight = (contentString?.stringHeightWithFont(UIFont.systemFont(ofSize: 15), contentWidth: SCREEN_W))!

            return creatJobContentTextHeight + 40
        }

        return 54.0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let identifier = "shopPlanCellIdentifier"
        var shopPlanCell = tableView.dequeueReusableCell(withIdentifier: identifier)
        if ((shopPlanCell == nil))
        {
            shopPlanCell = UITableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: identifier)
            
        }
      
        shopPlanCell?.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
        if isHaveContent && (indexPath as NSIndexPath).row == 2{
        
        shopPlanCell?.accessoryType = UITableViewCellAccessoryType.none
            
        }
        shopPlanCell?.selectionStyle = .none
        shopPlanCell?.textLabel?.text = arrOfRewardDetail[(indexPath as NSIndexPath).row]
        shopPlanCell?.textLabel?.numberOfLines = 0
        return shopPlanCell!
        
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        switch (indexPath as NSIndexPath).row{
            
        case 0,3: // add the selectTime View
            
            addSelectTimeViewWithTag((indexPath as NSIndexPath).row)

        case 1:
            
        let inputContentController =  IPInputContentController ()
        inputContentController.iputControlTitleString = "请填写内容"
        navigationController?.pushViewController(inputContentController, animated: true)
            
        default:
            print("clicked on the \((indexPath as NSIndexPath).row)")
        }
    }
    
    func addSelectTimeViewWithTag(_ selectTimeTag:NSInteger){
    
        jobPlanclickedTimeIndex = selectTimeTag
        setUpSelectTimeContentView()
    
    }
    
}
